package jlxy.ise.eroad.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import jlxy.ise.eroad.R;
import jlxy.ise.eroad.adapter.MainFragmentPageAdapter;
import jlxy.ise.eroad.fragment.LocationFragment;
import jlxy.ise.eroad.fragment.MyFragment;
import jlxy.ise.eroad.fragment.MyOrderFragment;
import jlxy.ise.eroad.fragment.WaitOrderFragment;


/**
 * 主页面
 */
public class TabMainActivity extends FragmentActivity {
    private ViewPager mMainViewPager;
    private List<Fragment> fragemnts = new ArrayList<>();
    private MainFragmentPageAdapter adapter;
    private RadioGroup mRg;
    //退出时间
    private long exitTime = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_view);
        initView();
        initData();
        setAdapter();
        setListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    //初始化控件
    private void initView() {
        mMainViewPager = (ViewPager) findViewById(R.id.main_viewpager);
        //设置viewpager的预加载页数，viewpager默认只会预加载左右两边的页面数据
        mMainViewPager.setOffscreenPageLimit(4);
        mRg = (RadioGroup) findViewById(R.id.main_rgp);
        //设置默认第一个为选中状态
        RadioButton rb = (RadioButton) mRg.getChildAt(0);
        rb.setChecked(true);
    }

    //初始化数据
    private void initData() {
        WaitOrderFragment waitOrderFragment = new WaitOrderFragment();
        MyOrderFragment myOrderFragment = new MyOrderFragment();
        LocationFragment locationFragment = new LocationFragment();
        MyFragment myFragment = new MyFragment();

        fragemnts.add(waitOrderFragment);
        fragemnts.add(myOrderFragment);
        fragemnts.add(locationFragment);
        fragemnts.add(myFragment);
    }

    //设置适配器
    private void setAdapter() {
        //实例化适配器
        adapter = new MainFragmentPageAdapter(getSupportFragmentManager(), fragemnts);
        //设置适配器
        mMainViewPager.setAdapter(adapter);
    }

    //设置监听
    private void setListener() {
        //viewPager的滑动监听
        mMainViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                //获取当前位置的RadioButton
                RadioButton rb = (RadioButton) mRg.getChildAt(position);
                //设置为true
                rb.setChecked(true);
            }
        });
        //RadioGroup的事件监听
        mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int index = 0; index < mRg.getChildCount(); index++) {
                    RadioButton rb = (RadioButton) mRg.getChildAt(index);
                    if (rb.isChecked()) {
                        mMainViewPager.setCurrentItem(index, false);
                        break;
                    }
                }
            }
        });
    }

    /**
     * 按两次退出应用
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(this, R.string.exit_again, Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
